#!/usr/bin/python
# -*- coding:utf-8 -*-
# @FileName : main.py
# Author    : myh
import function as f
import math
import json
# import matplotlib


contents = f.f_data_in('trace_analysis.log')
# print(contents)
#

[id_infos,a_num,b_num,all_time] = f.analysis_file(contents)
A_times =[]
A_arr = 0
B_times =[]
B_arr = 0

AB_time =[]
AB_arr =0
for id_info in id_infos:
    if id_info.pa_end_time > id_info.pa_start_time:
        A_times.append(id_info.pa_end_time - id_info.pa_start_time)
        A_arr += 1
    elif id_info.pa_end_time == id_info.pa_start_time:
        pass
    else:
        print(f"fram_id:{id_info.frame_id}, pa_s:{id_info.pa_start_time},pa_e:{id_info.pa_end_time}")
        a_num -= a_num

    if id_info.pb_end_time > id_info.pb_start_time:
        B_times.append(id_info.pb_end_time - id_info.pb_start_time)
        B_arr += 1
    elif id_info.pb_end_time == id_info.pb_start_time:
        pass
    else:
        print(f"fram_id:{id_info.frame_id}, pa_s:{id_info.pb_start_time},pa_e:{id_info.pb_end_time}")
        b_num -= b_num

    if id_info.bend_time != [0,0,0] and id_info.astart_time != [0,0,0]:
        AB_arr += 1
        id_info.bend_astart_time = (id_info.bend_time[0] - id_info.astart_time[0])*60 + (id_info.bend_time[1] - id_info.astart_time[1]) + (id_info.bend_time[2]-id_info.astart_time[2])/1000000
        AB_time.append(id_info.bend_astart_time)


A_time_avr = sum(A_times)/A_arr
B_time_avr = sum(B_times)/B_arr
print(f"A个数{A_arr}=={a_num}ProcessA的平均耗时为：{sum(A_times)/(A_arr)}")
print(f"B个数{B_arr}=={b_num}ProcessB的平均耗时为：{sum(B_times)/(B_arr)}")

A_times_s = A_times[:]
A_times_s.sort()
A_arr_90 = math.floor(A_arr*0.9)
B_times_s = B_times[:]
B_times_s.sort()
B_arr_90 = math.floor(B_arr*0.9)

A_time_p90 = A_times_s[A_arr_90-1]
B_time_p90 = B_times_s[B_arr_90-1]
print(f"A个数{A_arr}ProcessA的P90耗时为：第{A_arr_90-1}个，值为：{A_times_s[A_arr_90-1]}")
print(f"B个数{B_arr}ProcessA的P90耗时为：第{B_arr_90-1}个，值为：{B_times_s[B_arr_90-1]}")


A_arr_99 = math.floor(A_arr*0.99)
B_arr_99 = math.floor(B_arr*0.99)

A_time_p99 = A_times_s[A_arr_99-1]
B_time_p99 = B_times_s[B_arr_99-1]
print(f"A个数{A_arr}ProcessA的P99耗时为：第{A_arr_99-1}个，值为：{A_times_s[A_arr_99-1]}")
print(f"B个数{B_arr}ProcessA的P99耗时为：第{B_arr_99-1}个，值为：{B_times_s[B_arr_99-1]}")


id_infos_len = len(id_infos)
frame_time = id_infos_len/all_time
print(f'frame = {id_infos_len} , all_time = {all_time}s, frame / time ={frame_time}ms')


mean_AB_time = sum(AB_time)/AB_arr*1000
print(f'ABmean:{mean_AB_time}s')
contents = [{"ProcessA's average time:": A_time_avr},
            {"ProcessB's average time:": B_time_avr},
            {"ProcessA's P90 time:": A_time_p90},
            {"ProcessB's P90 time:": B_time_p90},
            {"ProcessA's P90 time:": A_time_p99},
            {"ProcessB's P90 time:": B_time_p99},
            {'tuntuliang：': frame_time},
            {"average time": mean_AB_time}]
contents = json.dumps(contents)
with open('result.json', "w", encoding='utf-8') as f:
    json.dump(contents, f, ensure_ascii=False)
